package dnl.jexem.camouflaj;

import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
import org.jasypt.util.text.BasicTextEncryptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Crypter {

	private static final Logger logger = LoggerFactory.getLogger(Crypter.class.getSimpleName());

	private static final String ENCRYPTOR_PASSWORD = "d5a4n3i2e1l";

	private static BasicTextEncryptor encryptor;

	static {
		encryptor = new BasicTextEncryptor();
		encryptor.setPassword(ENCRYPTOR_PASSWORD);
	}

	public static void main(String[] args) {
		if (args.length != 1) {
			printUsageAndExit();
		}
		String encryptPassword = encrypt(args[0]);
		System.out.println(encryptPassword);
	}

	public static String encrypt(String data) {
		return encryptor.encrypt(data);
	}

	public static String decrypt(String encriptedData) {
		try {
			return encryptor.decrypt(encriptedData);
		} catch (EncryptionOperationNotPossibleException e) {
			e.printStackTrace();
			logger.error("Given data is probably not encrypted. Exception message was:{}", e
					.getMessage());
			return null;
		}
	}

	private static void printUsageAndExit() {
		System.err.println("Usage: PasswordUtil [rawPassword]");
		System.exit(1);
	}
}
